package com.yuyou.tas.terminal.controller;

import com.yuyou.tas.service.api.admin.entity.AuthUser;
import com.yuyou.tas.service.api.feedback.dto.FeedbackClassPage;
import com.yuyou.tas.service.api.feedback.entity.Feedback;
import com.yuyou.tas.service.api.feedback.entity.FeedbackClass;
import com.yuyou.tas.service.api.feedback.service.FeedbackAPI;
import com.yuyou.tas.service.api.feedback.service.FeedbackCommentAPI;
import com.yuyou.tas.service.api.feedback.service.FeedbackNoteAPI;
import com.yuyou.tas.service.api.feedback.vo.req.FeedBackEditReqVo;
import com.yuyou.tas.service.api.feedback.vo.resp.FeedbackClassesVo;
import com.yuyou.tas.service.api.feedback.vo.resp.FeedbackEditDetail;
import com.yuyou.tas.terminal.vo.ReqMTBase;
import com.yuyou.tas.terminal.vo.ReqMU.FeedbackClassReqVo;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.enums.feedback.FeedbackCommentEnums;
import com.yuyou.tas.util.tool.LogUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api(value="YyfnMTFeedbackController",description="教师端反馈接口")
@Validated
@RestController
@RequestMapping(value = "/api/m/t/feedback")
public class YyfnMTFeedbackController extends YyfnMTController{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private FeedbackAPI feedbackAPI;

    @Autowired
    private FeedbackCommentAPI feedbackCommentAPI;

    @Autowired
    private FeedbackNoteAPI feedbackNoteAPI;
	
	@ApiOperation(value = "新增班级反馈信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "classId", value = "班级ID", paramType = "query", required = true),
			@ApiImplicitParam(name = "urls", value = "笔记图片Url,多个用逗号隔开,非必填", paramType = "query")})
	@RequestMapping(value = "/addFeedbackClass", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<FeedbackClass> addFeedbackClass(
			@Validated @ModelAttribute ReqMTBase reqMTBase,@NotNull(message = "班级ID不能为空") Long classId) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(reqMTBase.getSysCode(), reqMTBase.getTeacherId(),reqMTBase.getAccessToken(),reqMTBase.getChannel());
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
        ResBoolSimpleInfo<FeedbackClass> resBoolSimpleInfo = feedbackAPI.addFeedbackClass(classId, authUser.getId() , authUser.getAuthOrganizationId());
        if(!resBoolSimpleInfo.isSuccess()){
            return ReturnResult.fail(resBoolSimpleInfo.getMessage());
        }
		return ReturnResult.success(resBoolSimpleInfo.getData());
	}


	@ApiOperation(value = "班级反馈信息列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "classId", value = "班级ID", paramType = "query", required = true)})
	@RequestMapping(value = "/findFeedbackClass", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<FeedbackClassPage> findFeedbackClass(
            @Validated @ModelAttribute FeedbackClassReqVo vo) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(vo.getSysCode(), vo.getTeacherId(),vo.getAccessToken(),vo.getChannel());
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
		ResBoolSimpleInfo<FeedbackClassPage> resBoolSimpleInfo = feedbackAPI.findFeedbackClass(vo.getClassId() , authUser.getId() , authUser.getAuthOrganizationId() , vo.getPageIndex() , vo.getPageSize());
		if(!resBoolSimpleInfo.isSuccess()){

		    return ReturnResult.fail(resBoolSimpleInfo.getMessage());
		}
		return ReturnResult.success(resBoolSimpleInfo.getData());
	}

	@ApiOperation(value = "保存/更新反馈编辑内容")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "accessToken", value = "accessToken", paramType = "query", required = true),
			@ApiImplicitParam(name = "sysCod", value = "系统编号", paramType = "query", required = true),
			@ApiImplicitParam(name = "openId", value = "openId", paramType = "query", required = true),
			@ApiImplicitParam(name = "teacherId", value = "教师ID", paramType = "query", required = true),
			@ApiImplicitParam(name = "channel", value = "类型(0：phone，1：pc)", paramType = "query", required = true)})
	@RequestMapping(value = "/addFeedback", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<Feedback> addFeedback(@Validated @ModelAttribute FeedBackEditReqVo vo,
														 @NotBlank(message = "accessToken不能为空")String accessToken,
														 @NotBlank(message = "sysCode不能为空")String sysCode,
														 @NotBlank(message = "openId不能为空")String openId,
														 @NotNull(message = "teacherId不能为空")Long teacherId,
														 Integer channel) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(sysCode, teacherId,accessToken,channel);
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
		vo.setUserId(authUser.getId());
		ResBoolSimpleInfo<Feedback> resBoolSimpleInfo = feedbackAPI.addOrUpdateFeedbackService(vo);
		if(!resBoolSimpleInfo.isSuccess()){
	        LogUtil.error(logger, resBoolSimpleInfo.getMessage());
			return ReturnResult.fail(resBoolSimpleInfo.getMessage());
		}
		return ReturnResult.success(resBoolSimpleInfo.getData());
	}


	@ApiOperation(value = "批量发布")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "feedbackClassId", value = "班级反馈ID", paramType = "query", required = true)})
	@RequestMapping(value = "/batchPublish", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<Void> batchPublish(
			@Validated @ModelAttribute ReqMTBase reqMTBase,@NotNull(message = "班级反馈ID不能为空") Long feedbackClassId) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(reqMTBase.getSysCode(), reqMTBase.getTeacherId(),reqMTBase.getAccessToken(),reqMTBase.getChannel());
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
		ResBoolSimpleInfo resBoolSimpleInfo = feedbackAPI.batchPublish(feedbackClassId);
		if(!resBoolSimpleInfo.isSuccess()){
			return ReturnResult.fail(resBoolSimpleInfo.getMessage());
		}
		return ReturnResult.success();
	}

	@ApiOperation(value = "反馈编辑页数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "feedbackClassId", value = "班级反馈ID", paramType = "query", required = true)})
	@RequestMapping(value = "/getFeedbackEditDetail", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<FeedbackEditDetail> getFeedbackEditDetail(
			@Validated @ModelAttribute ReqMTBase reqMTBase,@NotNull(message = "班级反馈ID不能为空") Long feedbackClassId) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(reqMTBase.getSysCode(), reqMTBase.getTeacherId(),reqMTBase.getAccessToken(),reqMTBase.getChannel());
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
        ResBoolSimpleInfo<FeedbackEditDetail> resBoolSimpleInfo = feedbackAPI.getFeedbackEditDetail(feedbackClassId , authUser.getId() , authUser.getAuthOrganizationId());
        if(!resBoolSimpleInfo.isSuccess()){
            return ReturnResult.fail(resBoolSimpleInfo.getMessage());
        }
        if(resBoolSimpleInfo.getData() != null){
            resBoolSimpleInfo.getData().setHead_img(authUser.getHeadImg());
        }
		return ReturnResult.success(resBoolSimpleInfo.getData());
	}


    @ApiOperation(value = "回复家长的回复信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feedbackId", value = "反馈ID", paramType = "query", required = true),
            @ApiImplicitParam(name = "comment", value = "回复内容", paramType = "query", required = true)})
    @RequestMapping(value = "/addFeedbackComment", method = { RequestMethod.GET,
            RequestMethod.POST })
    public ReturnResult<Void> addFeedbackComment(
                                                @Validated @ModelAttribute ReqMTBase reqMTBase,
                                                @NotNull(message = "反馈ID不能为空") Long feedbackId,
                                                @NotBlank(message = "内容不能为空") String comment) throws Exception {
        //转换老师用户ID
        AuthUser authUser = this.getTasUser(reqMTBase.getSysCode(), reqMTBase.getTeacherId(),reqMTBase.getAccessToken(),reqMTBase.getChannel());
        if(authUser == null){
            ReturnResult.fail("找不到用户信息,请联系管理员。");
        }
        feedbackCommentAPI.saveFeedbackComment(feedbackId , comment , authUser.getAuthOrganizationId() , authUser.getId(),FeedbackCommentEnums.userType.tea.getValue());
        return ReturnResult.success();
    }


	@ApiOperation(value = "添加班级反馈优秀学生笔记")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "feedbackClassId", value = "班级反馈ID", paramType = "query", required = true),
			@ApiImplicitParam(name = "urls", value = "笔记图片Url,多个用；号隔开，微信图片ID和微信url用英文逗号隔开", paramType = "query")})
	@RequestMapping(value = "/saveFeedbackNote", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<Void> saveFeedbackNote(
			@Validated @ModelAttribute ReqMTBase reqMTBase,
			@NotNull(message = "班级反馈ID不能为空") Long feedbackClassId,
			String urls) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(reqMTBase.getSysCode(), reqMTBase.getTeacherId(),reqMTBase.getAccessToken(),reqMTBase.getChannel());
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
		ResBoolSimpleInfo resBoolSimpleInfo = feedbackNoteAPI.addFeedbackNote(feedbackClassId , urls, authUser.getAuthOrganizationId() , authUser.getId());
		if(!resBoolSimpleInfo.isSuccess()){
			return ReturnResult.fail(resBoolSimpleInfo.getMessage());
		}
		return ReturnResult.success();
	}

	/*@ApiOperation(value = "删除班级反馈优秀学生笔记")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "feedbackNoteId", value = "优秀笔记ID不能为空", paramType = "query", required = true)})
	@RequestMapping(value = "/delFeedbackNote", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<Void> delFeedbackNote(
			@Validated @ModelAttribute ReqMTBase reqMTBase,
			@NotNull(message = "优秀笔记ID不能为空") Long feedbackNoteId) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(reqMTBase.getSysCode(), reqMTBase.getTeacherId());
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
		return feedbackNoteAPI.delFeedbackNote(feedbackNoteId);
	}*/

	@ApiOperation(value = "获取老师所有授课班级的全部反馈")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "accessToken", value = "accessToken", paramType = "query", required = true),
			@ApiImplicitParam(name = "sysCod", value = "系统编号", paramType = "query", required = true),
			@ApiImplicitParam(name = "openId", value = "openId", paramType = "query", required = true),
			@ApiImplicitParam(name = "teacherId", value = "教师ID", paramType = "query", required = true),
			@ApiImplicitParam(name = "trimesterId", value = "学期ID", paramType = "query", required = true),
			@ApiImplicitParam(name = "channel", value = "类型(0：phone，1：pc)", paramType = "query", required = true)})
	@RequestMapping(value = "/findAllClassFeedback", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<List<FeedbackClassesVo>> findAllClassFeedback(@Validated @NotBlank(message = "accessToken不能为空")String accessToken,
																	  @NotBlank(message = "sysCode不能为空")String sysCode,
																	  @NotBlank(message = "openId不能为空")String openId,
																	  @NotNull(message = "teacherId不能为空")Long teacherId,
																	  @NotNull(message = "trimesterId不能为空")Long trimesterId,
																	  Integer channel) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(sysCode, teacherId,accessToken,channel);
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
		ResBoolSimpleInfo<List<FeedbackClassesVo>> resBoolSimpleInfo = feedbackAPI.findAllClassFeedback(authUser.getId() , trimesterId);
		if(!resBoolSimpleInfo.isSuccess()){
			LogUtil.error(logger, resBoolSimpleInfo.getMessage());
			return ReturnResult.fail(resBoolSimpleInfo.getMessage());
		}
		return ReturnResult.success(resBoolSimpleInfo.getData());
	}

	@ApiOperation(value = "获取老师所有未读反馈回复消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "accessToken", value = "accessToken", paramType = "query", required = true),
			@ApiImplicitParam(name = "sysCode", value = "系统编号", paramType = "query", required = true),
			@ApiImplicitParam(name = "openId", value = "openId", paramType = "query", required = true),
			@ApiImplicitParam(name = "teacherId", value = "教师ID", paramType = "query", required = true),
			@ApiImplicitParam(name = "channel", value = "类型(0：phone，1：pc)", paramType = "query", required = true)})
	@RequestMapping(value = "/getTeacherUnReadCount", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ReturnResult<Integer> getTeacherUnReadCount(@Validated @NotBlank(message = "accessToken不能为空")String accessToken,
																	  @NotBlank(message = "sysCode不能为空")String sysCode,
																	  @NotBlank(message = "openId不能为空")String openId,
																	  @NotNull(message = "teacherId不能为空")Long teacherId,
																	  Integer channel) throws Exception {
		//转换老师用户ID
		AuthUser authUser = this.getTasUser(sysCode, teacherId,accessToken,channel);
		if(authUser == null){
			ReturnResult.fail("找不到用户信息,请联系管理员。");
		}
		Integer count = feedbackCommentAPI.getTeacherUnReadCount(authUser.getId());
		return ReturnResult.returnRes(true , null , count);
	}


    @ApiOperation(value = "反馈回复设置为已读")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "accessToken", paramType = "query", required = true),
            @ApiImplicitParam(name = "sysCode", value = "系统编号", paramType = "query", required = true),
            @ApiImplicitParam(name = "openId", value = "openId", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师ID", paramType = "query", required = true),
            @ApiImplicitParam(name = "feedbackId", value = "反馈ID", paramType = "query", required = true),
            @ApiImplicitParam(name = "channel", value = "类型(0：phone，1：pc)", paramType = "query", required = true)})
    @RequestMapping(value = "/set2Read", method = { RequestMethod.GET,
            RequestMethod.POST })
    public ReturnResult<Integer> set2Read(@Validated @NotBlank(message = "accessToken不能为空")String accessToken,
                                                       @NotBlank(message = "sysCode不能为空")String sysCode,
                                                       @NotBlank(message = "openId不能为空")String openId,
                                                       @NotNull(message = "feedbackId不能为空")Long feedbackId,
                                                        @NotNull(message = "teacherId不能为空")Long teacherId,
                                                       Integer channel) throws Exception {
        //转换老师用户ID
        AuthUser authUser = this.getTasUser(sysCode, teacherId,accessToken,channel);
        if(authUser == null){
            ReturnResult.fail("找不到用户信息,请联系管理员。");
        }
        feedbackAPI.set2Read(feedbackId , authUser.getId());
        return ReturnResult.success();
    }



}
